/**
 *  This file is part of AugeasWH.
 *
 *  AugeasWH. is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  AugeasWH. is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with AugeasWH.  If not, see <http://www.gnu.org/licenses/>.
 */

package cz.fit.vutbr.pis2012.augeaswh.control.whman.slot_stock_items;

import java.io.Serializable;
import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import javax.ejb.EJB;
import javax.faces.bean.SessionScoped;
import javax.faces.bean.ManagedBean;

import cz.fit.vutbr.pis2012.augeaswh.managers.SlotsManager;
import cz.fit.vutbr.pis2012.augeaswh.persistence.Slot;
import cz.fit.vutbr.pis2012.augeaswh.persistence.SlotsStockItem;
import cz.fit.vutbr.pis2012.augeaswh.persistence.StockItem;

@ManagedBean
@SessionScoped
public class SlotStockItemEditorBean implements Serializable {

	private static final long serialVersionUID = -5341389577967270487L;
	
	@EJB
	private SlotsManager sm;
	
	private StockItem stockItem;
	private SlotsStockItem slotsStockItem;
	
	private Slot slot;
	private Timestamp placingDate;
	private BigDecimal ammount;
	private final Locale locale;
	private final String calendarPattern;

	public SlotStockItemEditorBean() {
		System.out.println("AAAAAA");
		stockItem = null;
		slotsStockItem = null;
		locale = Locale.GERMANY;
		calendarPattern = "MMM d, yyyy";
	}
	
	public String setEditorObjects(StockItem stockItem, SlotsStockItem slotStockItem) {
		setStockItem(stockItem);
		setSlotsStockItem(slotStockItem);
		return "edit_slot_stock";
	}
	
	public void setStockItem(StockItem stockItem) {
		if (stockItem == null) 
			throw new IllegalArgumentException("stockItem");
		this.stockItem = stockItem;
	}
	
	public void setSlotsStockItem(SlotsStockItem slotStockItem) {
		this.slotsStockItem = (slotStockItem == null) ? new SlotsStockItem() : slotStockItem;
		if (this.slotsStockItem.getPlacingDate() == null) {
			this.slotsStockItem.setPlacingDate(new Timestamp(new Date().getTime()));
		}
		slot = this.slotsStockItem.getSlot();
		placingDate = this.slotsStockItem.getPlacingDate();
		ammount = this.slotsStockItem.getAmmount();
	}
	
	public String submit() {
		if (stockItem != null && slotsStockItem != null) {
			slotsStockItem.setSlot(slot);
			slotsStockItem.setPlacingDate(placingDate);
			slotsStockItem.setAmmount(ammount);
			if (slotsStockItem.getId() == null) {
				stockItem.addSlotStockItem(slotsStockItem);
				slotsStockItem.setStockItem(stockItem);
			}
		}
		slotsStockItem = null;
		return "back";
	}
	
	public String cancel() {
		slotsStockItem = null;
		return "back";
	}
	
	public List<Slot> getSlots() {
		return sm.getSlots();
	}

	public Slot getSlot() {
		return slot;
	}

	public void setSlot(Slot slot) {
		this.slot = slot;
	}

	public Date getPlacingDate() {
		return new Date(placingDate.getTime());
	}

	public void setPlacingDate(Date placingDate) {
		System.out.println("Set: " + placingDate);
	}

	public BigDecimal getAmmount() {
		return ammount;
	}

	public void setAmmount(BigDecimal ammount) {
		this.ammount = ammount;
	}

	public Locale getCalendarLocale() {
		return locale;
	}

	public String getCalendarPattern() {
		return calendarPattern;
	}

	public String getTitle() {
		if (slotsStockItem == null || slotsStockItem.getId() == null) 
			return "Fill in form to create new slot stock item";
		return "Fill in form to edit selected slot stock item";
	}
}
